package com.zl.controller;

import com.zl.pojo.Dept;
import com.zl.pojo.Emp;
import com.zl.pojo.FenYe;
import com.zl.service.DeptService;
import com.zl.service.EmpService;
import com.zl.service.EmpUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ljm
 * @Date 2021/11/23 11:35
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService es;
    @Autowired
    private DeptService ds;
    @Autowired
    private EmpUploadService eu;

    @RequestMapping("/toMain")
    public String toMain(FenYe fenYe, Map<String,Object> req){
        List<Emp> emps = es.queryAllEmpByFenYe(fenYe);
        List<Dept> depts = ds.queryAllDept();
        req.put("emps",emps);
        req.put("depts",depts);
        return "/emp/main";
    }

    /*以json格式返回接口数据*/
    @RequestMapping("/toMainJson")
    @ResponseBody
    public Map<String,Object> toMainJson(FenYe fenYe){
        Map<String,Object> req =new HashMap<>();
        List<Emp> emps=es.queryAllEmpByFenYe(fenYe);
        List<Dept> depts=ds.queryAllDept();
        req.put("emps",emps);
        req.put("depts",depts);
        return req;
    }

    @RequestMapping("/toAdd")
    public String toAdd(Map<String,Object> req){
        List<Emp> mgrs=es.queryAllMgrEmp();
        //查询所有的部门
        List<Dept> depts=ds.queryAllDept();
        req.put("mgrs",mgrs);
        req.put("depts",depts);
        return "/emp/add";
    }

    @RequestMapping("/addEmp")  // 需要准确的赋值
    public String addEmp(Emp emp, @RequestParam("userImg")CommonsMultipartFile userImg) throws IOException {
        emp = eu.upload(emp,userImg);
        es.addEmp(emp);
        return "redirect:/emp/toMain";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(int empNo,Map<String,Object> req){
        //查询所有的经理级别员工
        List<Emp> mgrs=es.queryAllMgrEmp();
        //查询所有的部门
        List<Dept> depts=ds.queryAllDept();
        //查询当前需要修改的员工信息,用户回显
        Emp e=es.queryEmpByEmpNo(empNo);
        req.put("mgrs",mgrs);
        req.put("depts",depts);
        req.put("emp",e);
        //跳转页面
        return "/emp/update";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp,@RequestParam("userImg") CommonsMultipartFile userImg) throws IOException {
        emp = eu.upload(emp,userImg);
        es.updateEmp(emp);
        return "redirect:/emp/toMain";
    }

    @RequestMapping("/deleteEmp")
    // 拦截器前preHandle
    public String deleteEmp(int[] empNos) {
        es.deleteEmp(empNos);
        //postHandle
        return "redirect:/emp/toMain";
    }
    //afterCompletion


    /*局部异常处理*/
    @ExceptionHandler
    public ModelAndView partException(Exception e){
        ModelAndView mv = new ModelAndView();
        if(e instanceof MaxUploadSizeExceededException){
            System.out.println("上传图片过大！");
            e.printStackTrace();
            mv.addObject("message","上传图片过大！");
        }
        mv.setViewName("redirect:/error.jsp");
        return mv;
    }
}
